package com.societyGames.flashForms
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;

public class HorizontalSplitContainer extends Sprite
{
  private var _desiredHeight:Number;
  private var _desiredWidth:Number;
  private var _topPercent:Number = Number.NaN;
  private var _topItem:DisplayObject;
  private var _splitter:Splitter;
  private var _bottomItem:DisplayObject;

  public function HorizontalSplitContainer(splitter:DisplayObject, upDownCursor:DisplayObject, topPercent:Number = 0.5)
  {
    if (splitter == null)
    {
      throw new ArgumentError("Argument \'splitter\' cannot be null.");
    }
    this._splitter = new Splitter(splitter, upDownCursor);
    this._splitter.x = 0;
    this._splitter.bounds = new Rectangle();
    this._splitter.droppedEventObject.addEventListener(splitterDroppedHandler);
    this.addChild(this._splitter);
    this._topPercent = topPercent;
    this._desiredWidth = width;
    this._desiredHeight = height;
    this.width = this._desiredWidth;
    this.height = this._desiredHeight;
  }

  public function set topPercent(value:Number):void
  {
    if (this._topPercent != value)
    {
      if (isNaN(value))
      {
        throw new ArgumentError("Value cannot be NaN.");
      }
      else if (value < 0)
      {
        value = 0;
      }
      else if (value > 1)
      {
        value = 1;
      }
      this._topPercent = value;
      recalculateHeight();
    }
  }

  public function get topPercent():Number
  {
    return this._topPercent;
  }

  public function get topItem():DisplayObject
  {
    return this._topItem;
  }

  public function set topItem(value:DisplayObject):void
  {
    if (this._topItem != value)
    {
      if (this._topItem != null)
      {
        this.removeChild(this._topItem);
      }
      this._topItem = value;
      if (this._topItem != null)
      {
        this._topItem.x = 0;
        this._topItem.width = this._desiredWidth;
        this.addChildAt(this._topItem, 0);
        recalculateHeight();
      }
    }
  }

  public function get bottomItem():DisplayObject
  {
    return this._bottomItem;
  }

  public function set bottomItem(value:DisplayObject):void
  {
    if (this._bottomItem != value)
    {
      if (this._bottomItem != null)
      {
        this.removeChild(this._bottomItem);
      }
      this._bottomItem = value;
      if (this._bottomItem != null)
      {
        this._bottomItem.x = 0;
        this._bottomItem.width = this._desiredWidth;
        this.addChildAt(this._bottomItem, 0);
        recalculateHeight();
      }
    }
  }

  public override function set height(value:Number):void
  {
    if (value != this._desiredHeight)
    {
      this._desiredHeight = value;
      recalculateHeight();
    }
  }

  private function recalculateHeight():void
  {
    var topItemHeight:Number = (this._desiredHeight - this._splitter.height) * this._topPercent;
    if (this._topItem != null)
    {
      this._topItem.y = 0;
      this._topItem.height = topItemHeight;
    }
    this._splitter.y = topItemHeight;
    this._splitter.bounds.height = this._desiredHeight;
    if (this._bottomItem != null)
    {
      this._bottomItem.y = this._splitter.y + this._splitter.height;
      this._bottomItem.height = this._desiredHeight - this._splitter.height - topItemHeight;
    }
  }

  public override function set width(value:Number):void
  {
    if (value != this._desiredWidth)
    {
      this._desiredWidth = value;
      if (this._topItem != null)
      {
        this._topItem.width = value;
      }
      this._splitter.width = value;
      if (this._bottomItem != null)
      {
        this._bottomItem.width = value;
      }
      //super.width = value;
    }
  }

  private function splitterDroppedHandler(event:Event):void
  {
    this.topPercent = this._splitter.y / (this._desiredHeight - this._splitter.height);
  }
}
}